package net.wangds.persistence.columnoriented.comp;

import net.wangds.jdbc.comp.jdbc.JdbcComp;
import net.wangds.log.helper.LogHelper;
import net.wangds.persistence.columnoriented.comp.AbstractColumnOrientedComp;
import net.wangds.persistence.columnoriented.conv.ToBeanConv;
import net.wangds.persistence.columnoriented.conv.ToColumnsConv;
import net.wangds.persistence.columnoriented.entity.ColumnOrientedEntity;
import net.wangds.persistence.columnoriented.repository.db.ColumnOrientedRepo;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

import java.util.Collections;

/**
 * .
 * <p>
 *     这是一个查询SQL的模板:<pre>
 * select t7.* from (
 * select * from (select row_key from (
 * select distinct row_key, count(0) as res1 from tcec102_col_operators t1
 * where (t1.field = '' and t1.int_val = 0) or (t1.field = '' and t1.int_val = 0)
 * ) t2 where t2.res1 = 2 ) t3 left join
 * (select * from tcec102_col_operators t4 where field='') t5
 * on (t3.row_key = t5.row_key) order by t5.int_val limit 0, 10
 * ) t6 left join tcec102_col_operators t7 on (t6.row_key = t7.row_key) ;
 * ;
 *     </pre>
 * </p>
 *
 * @author 王东石 2020/11/9 1:03.
 */
public abstract class AbstractColOriOpComp<C extends ColumnOrientedEntity, B> extends AbstractColumnOrientedComp<C, B> {

    @Autowired
    @SuppressWarnings("unused")
    private JdbcComp mmJdbcComp;

    /**
     * 构造函数.
     *
     * @param repo       Repo对象.
     * @param toBeanConv 从列数据列表转换为Bean的转换器.
     * @param toColsConv 从Bean实例转换为列字段记录列表的转换器.
     */
    public <R extends ColumnOrientedRepo<C>> AbstractColOriOpComp(R repo, ToBeanConv<C, B> toBeanConv, ToColumnsConv<B, C> toColsConv) {
        super(repo, toBeanConv, toColsConv);
    }


    /**
     * 不推荐使用,尚未完工.
     * @param pg 分页.
     * @param args 参数
     * @param <T> 类型.
     * @return 查询分页结果.
     */
    @Deprecated
    protected <T> Page<T> findBy(Pageable pg, Object ... args){

        Throwable th = new Throwable();
        if(pg==null){
            pg = PageRequest.of(0,10);
        }
        StackTraceElement[] sts = th.getStackTrace();
        if(sts==null){
            return new PageImpl<>(Collections.emptyList(), pg, 0);
        }

        if(sts.length<=1){
            throw new RuntimeException("不能调用findBy方法的通用实现.");
        }

        //通过调用堆栈可以获得调用本方法的代码所在方法名.
        StackTraceElement st = th.getStackTrace()[1];
        String methodName = st.getMethodName(); //方法名

        LogHelper.dev(">>>>>>>>>>>>>>>>>>");
        LogHelper.dev("methodName:"+methodName);

        String sql = buildSql(methodName, pg);
        LogHelper.dev("sql:"+sql);

        LogHelper.dev("<<<<<<<<<<<<<<<<<<");


        return new PageImpl<>(Collections.emptyList(), pg, 0);

    }

    /**
     * 构建查询SQL.
     * @param methodName 方法名.
     * @return SQL.
     */
    private String buildSql(String methodName, Pageable pg){

        if(StringUtils.startsWith(methodName, "findBy")){
            String strCondition = methodName.substring(6);
            strCondition = RegExUtils.replaceAll(strCondition, "_", "( ");
            strCondition = RegExUtils.replaceAll(strCondition, "\\$", ") ");
            strCondition = RegExUtils.replaceAll(strCondition, "And", " and ");
            strCondition = RegExUtils.replaceAll(strCondition, "Or", " or ");


            LogHelper.dev("condition str:"+strCondition);
        }

        return "";

    }


}
